import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:delivery_application/common/globs.dart';
import 'package:delivery_application/common/locator.dart';
import 'package:delivery_application/common/my_http_overrides.dart';
import 'package:delivery_application/common/service_call.dart';
import 'package:delivery_application/view/home/home_page.dart';
import 'package:delivery_application/view/home/start_deliver.dart';
import 'package:delivery_application/view/login/login_view.dart';
import 'package:delivery_application/view/login/welcome_view.dart';
import 'package:delivery_application/view/on_boarding/startup_view.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  HttpOverrides.global = MyHttpOverrides();
  
  // 初始化SharedPreferences
  await SharedPreferences.getInstance();
  
  // 配置加载动画
  configLoading();
  
  runApp(const MyApp());
}

//设置加载动画的外观和行为的函数
void configLoading() {
  EasyLoading.instance
    ..indicatorType = EasyLoadingIndicatorType.ring
    ..loadingStyle = EasyLoadingStyle.custom
    ..indicatorSize = 45.0
    ..radius = 8.0
    ..progressColor = Colors.yellow
    ..backgroundColor = Colors.green
    ..indicatorColor = Colors.yellow
    ..textColor = Colors.yellow
    ..userInteractions = false
    ..dismissOnTap = false;
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '快递订单APP',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        fontFamily: "Metropolis",
        colorScheme: ColorScheme.fromSeed(seedColor: Color(0xff00AEEE)),
        primaryColor: Color(0xff00AEEE),
        useMaterial3: true,
      ),

      onGenerateRoute: (RouteSettings settings) {
        switch (settings.name) {
          case "welcome":
            return MaterialPageRoute(builder: (context) => const WelcomeView());
          case "home":
            return MaterialPageRoute(builder: (context) => const StartDeliver());
          case "login":
            return MaterialPageRoute(builder: (context) => const LoginView());
          default:
            return MaterialPageRoute(
                builder: (context) => const Scaffold(
                        body: Center(
                      child: Text("找不到对应页面"),
                    )));
        }
      },
      
      // 设置初始页面为StartupView
      home: const StartupView(),
      
      builder: (context, child) {
        return FlutterEasyLoading(child: child);
      },
    );
  }
}
